/**
 * =================================================================
 * 版权所有 2011-2013 深圳市泰海网络科技服务有限公司，并保留所有权利
 * -----------------------------------------------------------------
 * 这不是一个自由软件！您不能在任何未经允许的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布
 * =================================================================
 */
package com.imoney.payment.trade.handler;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.imoney.framework.base.entity.IRequest;
import com.imoney.framework.base.entity.Response;
import com.imoney.framework.base.enums.TradeType;
import com.imoney.framework.base.exception.ServiceException;
import com.imoney.payment.account.domain.Account;
import com.imoney.payment.account.enums.AccountStatus;
import com.imoney.payment.account.service.IAccountQueryService;
import com.imoney.payment.chain.HandlerChain;
import com.imoney.payment.trade.common.InfoCode;
import com.imoney.payment.trade.domain.PayRequest;
import com.imoney.payment.trade.domain.PaymentItem;
import com.imoney.payment.trade.domain.TradeOrder;
import com.imoney.payment.trade.service.ITradeQueryService;

/**
 * 类说明：检查账号<br>
 * 
 * <p>
 * 详细描述：<br>
 * 
 * </p>
 * 
 * <pre>
 * ——————————————————————————————————————————————————————————————————
 * |		修改人		|		修改时间			|		修改原因
 * ——————————————————————————————————————————————————————————————————
 * |	zengxx 曾宪新	|		2013-6-6		|	
 * ——————————————————————————————————————————————————————————————————
 * </pre>
 * 
 * @author zengxx 曾宪新(Xavier.zeng)
 * 
 *         CreateDate: 2013-6-6
 */
@Service
public class PayAccountHandler extends AbstractHandler<List<TradeOrder>> {

	@Resource
	private ITradeQueryService tradeQueryService;

	@Resource
	private IAccountQueryService accountQueryService;

	@Override
	public void doHandler(HandlerChain<List<TradeOrder>> chain,
			IRequest request, Response<List<TradeOrder>> response)
			throws ServiceException {
		PayRequest payRequest = (PayRequest) request;
		List<PaymentItem> PaymentItems = payRequest.getPaymentList();

		List<TradeOrder> tradeList = response.getData();
		Account fromAccount = accountQueryService.queryAccountByNo(tradeList
				.get(0).getBuyerEmail(), "");
		if (AccountStatus.CANCEL.equals(fromAccount.getStatus())) {
			response.setCode(InfoCode.ACT_CANCELED);
			return;
		}
		if (AccountStatus.FREEZE.equals(fromAccount.getStatus())) {
			response.setCode(InfoCode.ACT_FREEZE_INVALID_PAY);
			return;
		}
		/** 金额不足 */
		if (fromAccount.getFreeAmt() <= 0) {

		}

		long totalAmt = 0;
		for (TradeOrder order : tradeList) {
			/**
			 * 如果不是担保交易，金额是直接到对方的账户，验证对方账户是否注销
			 */
			if (TradeType.ASSURE.equals(order.getTradeType())) {

			}

			totalAmt += order.getAmt();
		}

		/** 金额不足以支付 */
		if (fromAccount.getFreeAmt() < totalAmt) {

		}

		/** 处理下一个任务 */
		chain.doHandler(chain, request, response);

		response.setMessage(response.getMessage() + "-----TradePayHandler");
	}
}
